package com.nulldev.util.scripting.ScriptAPI.baseJS.nsb;

import java.util.List;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;

import com.nulldev.util.internal.backport.concurrency9.Lists;
import com.nulldev.util.scripting.ScriptAPI.baseJS.BaseJS;

public class BaseJSSEFactory implements ScriptEngineFactory {

	private static final List<String> names = immutableList("basejs", "base.js", "BaseJS", "nullUtil's BaseJS", "js", "JS", "JavaScript", "javascript",
			"ECMAScript", "ecmascript");
	private static final List<String> mimeTypes = immutableList("application/javascript", "application/ecmascript", "text/javascript", "text/ecmascript");
	private static final List<String> extensions = immutableList("js");

	private static List<String> immutableList(String... elements) {
		return Lists.of(elements);
	}

	@Override
	public String getEngineName() {
		return "BaseJS";
	}

	@Override
	public String getEngineVersion() {
		return String.valueOf(BaseJS.VERSION);
	}

	@Override
	public List<String> getExtensions() {
		return extensions;
	}

	@Override
	public List<String> getMimeTypes() {
		return mimeTypes;
	}

	@Override
	public List<String> getNames() {
		return names;
	}

	@Override
	public String getLanguageName() {
		return "javascript";
	}

	@Override
	public String getLanguageVersion() {
		return "1.0.0";
	}

	@Override
	public Object getParameter(final String key) {
		if (key.equals("javax.script.engine_version")) {
			return this.getEngineVersion();
		} else if (key.equals("javax.script.engine")) {
			return this.getEngineName();
		} else if (key.equals("javax.script.language_version")) {
			return this.getLanguageVersion();
		} else if (key.equals("javax.script.language_name")) {
			return "ECMAScript";
		} else if (key.equals("THREADING")) {
			return false;
		} else
			return null;
	}

	@Override
	public String getMethodCallSyntax(final String obj, final String m, final String... args) {
		return null;
	}

	@Override
	public String getOutputStatement(final String toDisplay) {
		return "console.log(" + toDisplay + ")";
	}

	@Override
	public String getProgram(final String... statements) {
		return null;
	}

	@Override
	public ScriptEngine getScriptEngine() {
		return new BaseJSSE();
	}

}
